package ruc.grs.ypytd.dao.db;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ResourceBundle;

import oracle.jdbc.pool.OracleConnectionPoolDataSource;

import org.apache.log4j.Logger;

public class OracleDataSource {
	final static Logger log = Logger.getLogger(OracleDataSource.class);
	static ResourceBundle apps = ResourceBundle
			.getBundle("ruc.grs.ypytd.db.oracleConfig");

	static String servername, dbname, port, username, passwd;

	private OracleConnectionPoolDataSource ocpds = null;
	private static OracleDataSource dataSource = null;

	static {
		servername = apps.getString("servername");
		dbname = apps.getString("databasename");
		username = apps.getString("username");
		passwd = apps.getString("password");
		port = apps.getString("port");

	}

	private OracleDataSource() {
		try {
			ocpds = new OracleConnectionPoolDataSource();

			ocpds.setDriverType("thin");
			ocpds.setServerName(servername);
			ocpds.setNetworkProtocol("tcp");
			ocpds.setDatabaseName(dbname);
			ocpds.setPortNumber(Integer.parseInt(port));
			ocpds.setUser(username);
			ocpds.setPassword(passwd);
		} catch (SQLException e) {
			e.printStackTrace();
			log.error(e);
		}
	}

	private Connection createConnection() throws SQLException {
		return ocpds.getConnection();
	}

	public static Connection getConnection() throws SQLException {
		if (dataSource == null) {
			synchronized (OracleDataSource.class) {
				if (dataSource == null) {
					dataSource = new OracleDataSource();
				}
			}
		}
		return dataSource.createConnection();
	}

}
